<template>
	<vk-data-dialog
		v-model="value.show"
		:title="page.title"
		:top="page.top"
		:width="page.width"
		:close-on-click-modal="true"
		mode="form"
		@open="onOpen"
		@closed="onClose"
	>
		<!-- 页面主体内容开始 -->
		<vk-data-form
			ref="form1"
			v-model="form1.data"
			:form-type="value.mode"
			:rules="form1.props.rules"
			:action="form1.props.action"
			:columns="form1.props.columns"
			:loading.sync="form1.props.loading"
			:labelWidth="form1.props.labelWidth"
			:show-cancel="page.showCancel"
			:cancel-text="page.cancelText"
			:submit-text="page.submitText"
			:auto-close="false"
			max-height="82vh"
			@success="onFormSuccess"
		>
			<template v-slot:userInfo="{ form, keyName }">
				<view>
					<view>
						 <el-avatar :size="20" :src="userInfo.avatar"></el-avatar>
						 <text style="margin-left: 5px;">{{ userInfo.nickname }}</text>
					</view>
					<view>余额：{{ vk.pubfn.priceFilter(userInfo.balance) }} 元</view>
				</view>
			</template>
		</vk-data-form>
		<!-- 页面主体内容结束 -->
	</vk-data-dialog>
</template>

<script>
var that; // 当前页面对象
var vk = uni.vk; // vk实例
export default {
	props: {
		value: {
			Type: Object,
			default: function() {
				return {
					show: false,
					mode: "",
					item: {}
				};
			}
		}
	},
	data: function() {
		// 组件创建时,进行数据初始化
		return {
			page: {
				title: "余额",
				submitText: "确定",
				cancelText: "关闭",
				showCancel: true,
				top: "3vh",
				width: "500px"
			},
			form1: {
				// 表单请求数据，此处可以设置默认值
				data: {
					_id:"",
					isAdd:true,
					updateTotal: true
				},
				// 表单属性
				props: {
					// 表单请求地址
					action: "admin/business/sys/user.updateUserBalance",
					// 表单字段显示规则
					columns: [
						{ key:"userInfo", title:"用户信息", type:"text" },
						// 充值方式
						{ key:"", title:"方式", type:"bar-title" },
						{
							key:"isAdd", title:"操作方式", type:"radio" ,
							data:[
								{ value:true, label:"增加" },
								{ value:false, label:"减少" }
							]
						},
						{
							key:"updateTotal", title:"是否影响累计", type:"radio" ,
							data:[
								{ value:true, label:"影响" },
								{ value:false, label:"不影响" }
							]
						},
						{	key:"value", title:"余额变动数量", type:"money" ,width:600, tips:"影响用户余额", required: true },
						{ key:"", title:"备注", type:"bar-title" },
						{
							key:"remark", title:"备注", type:"textarea" , width:600 ,
							autosize:{ minRows:4, maxRows:10 },
							maxlength:200,
							showWordLimit:true,
						},
					],
					// 表单验证规则
					rules: {
						isAdd:[
							{ required: true, message: '请选择操作方式', trigger: ['blur','change'] },
						],
						value:[
							{ required: true, message: '余额数量不能为空', trigger: ['blur','change'] },
						]
					},
					// 左侧label的宽度
					labelWidth: "120px"
				}
			},
			userInfo:{}
		};
	},
	mounted() {
		that = this;
		that.init();
	},
	methods: {
		// 初始化
		init() {
			let { value } = that;
			that.$emit("input", value);
		},
		// 监听 - 页面打开
		onOpen() {
			that = this;
			let { item={} } = that.value;
			// 每次打开时，重新设置表单的值 = value.item 的值，item通过 vk.pubfn.openForm('表单名',{ item:{ _id:"1" } }) 传递值
			that.userInfo = {
				nickname: item.nickname,
				avatar: item.avatar,
				balance: item.account_balance.balance
			};
			that.form1.data._id = item._id;
		},
		// 监听 - 页面关闭
		onClose() {
			//that.$refs.form1.resetForm(); // 关闭时，重置表单
		},
		// 监听 - 提交成功后
		onFormSuccess(res) {
			that.value.show = false; // 关闭页面
			that.value.item.account_balance = res.data.userInfo.account_balance;
			that.$emit("success");
		}
	},
	watch: {
		
	},
	// 计算属性
	computed: {}
};
</script>

<style lang="scss" scoped></style>
